// import { Form } from 'element-ui'
import Vue from 'vue'
import VueRouter from 'vue-router'
// 挂载登录路由
// import Login from '../components/login.vue'
const Login = () =>
  import(/* webpackChunkName: "login_home_welcome" */ '../components/login.vue')
  // 挂载主页路由
  // import Home from '../components/home.vue'
const Home = () =>
  import(/* webpackChunkName: "login_home_welcome" */ '../components/home.vue')
  // 挂载main区域路由
  // import Welcome from '../components/welcome.vue'
const Welcome = () =>
  import(/* webpackChunkName: "login_home_welcome" */ '../components/welcome.vue')
  // 用户管理
  // import Users from '../components/users.vue'
const Users = () =>
  import(/* webpackChunkName: "Users_Rights_Roles" */ '../components/users.vue')
  // 权限管理
  // 1权限列表
  // import Rights from '../components/rights.vue'
const Rights = () =>
  import(/* webpackChunkName: "Users_Rights_Roles" */ '../components/rights.vue')
  // 2角色列表
  // import Roles from '../components/roles.vue'
const Roles = () =>
  import(/* webpackChunkName: "Users_Rights_Roles" */ '../components/roles.vue')
  // 商品管理
  // 1商品分类
  // import Cate from '../components/goods/cate.vue'
const Cate = () =>
  import(/* webpackChunkName: "Cate_Params" */ '../components/goods/cate.vue')
  // 2分类参数
  // import Params from '../components/goods/params.vue'
const Params = () =>
  import(/* webpackChunkName: "Cate_Params" */ '../components/goods/params.vue')
  // 3商品列表
  // import List from '../components/goods/list.vue'
const GoodsList = () =>
  import(/* webpackChunkName: "GoodsList_Add" */ '../components/goods/list.vue')
  // import Add from '../components/goods/add.vue'
const Add = () =>
  import(/* webpackChunkName: "GoodsList_Add" */ '../components/goods/add.vue')
  // 订单管理
  // import Orders from '../components/order/order.vue'
const Order = () =>
  import(/* webpackChunkName: "Order_Report" */ '../components/order/order.vue')
  // 数据统计
  // import Reports from '../components/report/report.vue'
const Report = () =>
  import(/* webpackChunkName: "Order_Report" */ '../components/report/report.vue')
Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    { path: '/', component: Login },
    { path: '/login', component: Login },
    {
      path: '/home',
      component: Home,
      redirect: '/welcome',
      children: [
        { path: '/home', component: Welcome },
        { path: '/users', component: Users },
        { path: '/rights', component: Rights },
        { path: '/roles', component: Roles },
        { path: '/categories', component: Cate },
        { path: '/params', component: Params },
        { path: '/goods', component: GoodsList },
        { path: '/goods/add', component: Add },
        { path: '/orders', component: Order },
        { path: '/reports', component: Report }
      ]
    }
  ]
})
// 挂载路由导航守卫  ?
router.beforeEach((to, from, next) => {
  // to     将要访问的路径
  // from   代表从哪个路径跳转而来
  // next   是一个函数表示放行
  // next() 放行  next("/login") 强制跳转
  if (to.path === '/login') return next()
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})

export default router
